package fr.emse.tatiana;




import java.io.File;

import org.eclipse.core.resources.*;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.model.WorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;
import org.eclipse.ui.part.ResourceTransfer;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.views.navigator.LocalSelectionTransfer;

/**
 * Base implemementation for a workspace viewer
 * 
 * @author dyke
 *
 */
public abstract class AbstractNavigationView extends ViewPart implements ISelectionChangedListener, IDoubleClickListener {

	protected TreeViewer viewer;
	protected Composite parent;
	
	protected abstract WorkbenchContentProvider getContentProvider();

	

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * i
	 */
	@Override
	public void createPartControl(Composite parent) {
		this.parent=parent;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns=1;
		gridLayout.marginHeight=0;
		gridLayout.marginWidth=0;
		parent.setLayout(gridLayout);


		viewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		viewer.setContentProvider(getContentProvider());
		viewer.setLabelProvider(new WorkbenchLabelProvider());
		viewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
		viewer.addSelectionChangedListener(this);
		viewer.addDoubleClickListener(this);
		/*
		int ops = DND.DROP_MOVE;
		Transfer[] transfers = new Transfer[] { ResourceTransfer.getInstance()};
		viewer.addDragSupport(ops, transfers,new MyDragListener(viewer));
		viewer.addDropSupport(ops, transfers,new MyDropListener(viewer));
		*/
		
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	@Override
	public void setFocus() {
		viewer.getControl().setFocus();
	}

	/**
	 * Call when a file has been changed
	 */
	public void update() {
		//TODO add a listener on the workspace?
		viewer.setInput(ResourcesPlugin.getWorkspace().getRoot());
	}

	/**
	 * @return the current selection on this view
	 */
	public IStructuredSelection getSelection() {
		return (IStructuredSelection) viewer.getSelection();
	}
	

	
}